﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Controls
{
	public class ListSearchItem<ListControlType> : SearchItem where ListControlType : Sunny.Controls.DropDownList
	{
		private Sunny.Controls.DropDownList _SimpleSearcherInput;
		public override System.Windows.FrameworkElement SimpleSearcherInput
		{
			get
			{
				if (this._SimpleSearcherInput == null)
				{
					this._SimpleSearcherInput = (Sunny.Controls.DropDownList)System.Activator.CreateInstance(typeof(ListControlType));
					this._SimpleSearcherInput.AllowNull = true;
					this._SimpleSearcherInput.SelectedIndex = 0;
				}
				return this._SimpleSearcherInput;
			}
		}

		//private System.Type _ListControlType;
		//[System.ComponentModel.TypeConverter(typeof(TypeTypeConverter))]
		//public System.Type ListControlType
		//{
		//    get
		//    {
		//        return this._ListControlType;
		//    }
		//    set
		//    {
		//        this._ListControlType = value;
		//        this.InputCell.Controls.Clear();
		//        this._SimpleSearcherInput = null;
		//        this.InputCell.Controls.Add(this.SimpleSearcherInput);
		//    }
		//}

		public object SelectedValue
		{
			get
			{
				if (this._SimpleSearcherInput == null)
					return null;
				return this._SimpleSearcherInput.SelectedValue;
			}
			set
			{

				this._SimpleSearcherInput.SelectedValue = value;
			}
		}

		/*
	{ Text: "包含", Value: "{0}.Contains({1})" },
	{ Text: "不包含", Value: "!{0}.Contains({1})" },
	{ Text: "等于", Value: "{0}=={1}" },
	{ Text: "始于", Value: "{0}.StartsWith({1})" },
	{ Text: "非始于", Value: "!{0}.StartsWith({1})" },
	{ Text: "终于", Value: "{0}.EndsWith({1})" },
	{ Text: "非终于", Value: "!{0}.EndsWith({1})" }
		 */
		private Operator[] _OperatorOptions;
		public override Operator[] Operators
		{
			get
			{
				if (this._OperatorOptions == null)
					this._OperatorOptions = new Operator[]
					{
						new Operator("=","{0}={1}"),
						new Operator("≠","{0}!={1}"),
						new Operator(">","{0}>{1}"),
						new Operator("≥","{0}>={1}"),
						new Operator("<","{0}<{1}"),
						new Operator("≤","{0}<={1}")
					};
				return this._OperatorOptions;
			}
		}

		public override System.Windows.FrameworkElement AdvanceSearcherInput
		{
			get
			{
				return (System.Windows.FrameworkElement)System.Activator.CreateInstance(typeof(ListControlType));
			}
		}

		public override Searcher.WhereCommand GetCommand(string field, int index)
		{
			if (this.SelectedValue == null)
				return null;
			return new Searcher.WhereCommand { Text = string.Format("{0}==@{1}", field, index), Parameters = new object[] { this.SelectedValue } };
		}

		public override void Empty()
		{
			//if (this._SimpleSearcherInput != null)
			this._SimpleSearcherInput.SelectedIndex = 0;
		}
	}
}